package tom_io_nio.bio;


import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * bio服务端
 * */
public class BioServer {

    ServerSocket serverSocket;

    public BioServer(int port){
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("服务端启动：监听端口：" + port);
        }catch (IOException e){
            e.printStackTrace();
        }

    }


    /**
     * 开始监听，处理逻辑
     * **/
    public void listener() throws Exception{
        //死循环监听
        while (true){
            //如果没有请求，不会往下执行
            Socket client = serverSocket.accept();//等待客户端连接，阻塞方法
            //拿到客户端的输入流
            InputStream inputStream = client.getInputStream();
            //缓冲区
            byte[] buff = new byte[1024];
            int len = inputStream.read(buff);
            if (len!=-1){
                String msg = new String(buff,0,len);
                System.out.println("收到：" + msg);
            }
        }
    }

    public static  void main(String[] args)throws Exception{
        new BioServer(8080).listener();
    }
}
